HEADS UP!

I’ve found issues with the data produced by code in this file, DO NOT USE IT!

You can find an updated version of the processed data (and the code to produce it) here

The old document starts here

The code for generating this document lives here, in the file index.Rmd.

Setup

Loading and installing packages in case it is necessary

list_of_packages <- c("tidyverse", "plotly", "shiny", "here", "DT")
new_packages <-
  list_of_packages[!(list_of_packages %in% installed.packages()[, "Package"])]
if (length(new_packages))
  install.packages(new_packages)
library(tidyverse)
library(plotly)
library(shiny)
library(here)
library(DT)

Defining the function I used to calculate distance

calc_flight_dist <- name <- function(dat) {
  dat_dist <-  {{dat}} %>% 
  dplyr::filter(!is.na(x_1)) %>%
  dist() %>% 
  as.matrix()

return(sum((dat_dist[row(dat_dist) == col(dat_dist) + 1])))
  
}

Defining function I used to calculate sinuosity (see explanation below)

Actually the following calculates the straight distance between the first and the last recorded points. sinuosity will be (as Camille defines it in the morpho paper) the ration between the actual distance and this straight distance.

calc_straight_flight_dist <- function(dat) {
  
dist_dat <- 
  {dat} %>% 
  dplyr::filter(!is.na(x_1)) %>%
  dist() %>% 
  as.matrix()

dist_dat[1, nrow(dist_dat)]

}

Defining function I used to calculate all variables from flight trajectory

calc_flight_trajectory_vars <- function(dat) {
  
dat %>% 
  group_by(butterfly_flight) %>% 
  summarise(flight_dist = calc_flight_dist(.),
            str_flight_dist = calc_straight_flight_dist(.),
            sinuosity = flight_dist / str_flight_dist,
            min_x = min(.$x_1, na.rm = T),
            max_x = max(.$x_1, na.rm = T),
            min_y = min(.$y_1, na.rm = T),
            max_y = max(.$y_1, na.rm = T),
            min_z = min(.$z_1, na.rm = T),
            max_z = max(.$z_1, na.rm = T))
}

Defining the function I used to make the 3d plots

plots_flight_path <- function(dat) {
  
plot_ly(data = dat,
        type = "scatter3d", 
        x = dat$x_1 ,
        y = dat$y_1,
        z = dat$z_1,
        mode = "lines")
}

Processing the CSV files

Creates the dataframe that will receive the data from the flight paths

flights <-
tibble::tibble(
  butterfly_flight = character(),
  flight_dist = numeric(),
  str_flight_dist = numeric(),
  sinuosity = numeric(),
  min_x = numeric(),
  max_x = numeric(),
  min_y = numeric(),
  max_y = numeric(),
  min_z = numeric(),
  max_z = numeric()
) 

b195 t1

Reads the data

b195_t1 <- readr::read_csv(here::here("unpaired-points", "b195-t1-unpaired-points-xyz.csv")) %>% 
  dplyr::mutate(butterfly_flight = "b195_t1") 

Calculates the variables

flights <- 
  calc_flight_trajectory_vars(b195_t1) %>% 
  dplyr::bind_rows(flights, .)

Plots the trajectory

plots_flight_path(b195_t1)

b195 t2

Reads the data

b195_t2 <- readr::read_csv(here::here("unpaired-points", "b195-t2-unpaired-points-xyz.csv")) %>% 
  dplyr::mutate(butterfly_flight = "b195_t2") 

Calculates the variables

flights <-
  calc_flight_trajectory_vars(b195_t2) %>%
  bind_rows(flights, .)

Plots the trajectory

plots_flight_path(b195_t2)

b195 t3

Reads the data

b195_t3 <- readr::read_csv(here::here("unpaired-points", "b195-t3-unpaired-points-xyz.csv")) %>% 
  dplyr::mutate(butterfly_flight = "b195_t3") 

Calculates the variables

flights <-
  calc_flight_trajectory_vars(b195_t3) %>%
  bind_rows(flights, .)

Plots the trajectory

plots_flight_path(b195_t3)

b195 t4

Reads the data

b195_t4 <- readr::read_csv(here::here("unpaired-points", "b195-t4-unpaired-points-xyz.csv")) %>% 
  dplyr::mutate(butterfly_flight = "b195_t4") 

Calculates the variables

flights <-
  calc_flight_trajectory_vars(b195_t4) %>%
  bind_rows(flights, .)
flights
## # A tibble: 4 x 10
##   butterfly_flight flight_dist str_flight_dist sinuosity   min_x  max_x min_y
##   <chr>                  <dbl>           <dbl>     <dbl>   <dbl>  <dbl> <dbl>
## 1 b195_t1                 5.57            2.45      2.27 -0.177  0.0149 -1.14
## 2 b195_t2                 3.75            2.23      1.68 -0.0269 0.437  -1.35
## 3 b195_t3                 3.88            2.12      1.83 -0.0153 0.469  -1.40
## 4 b195_t4                 3.98            2.96      1.34 -0.0902 0.0290 -1.60
## # … with 3 more variables: max_y <dbl>, min_z <dbl>, max_z <dbl>

Plots the trajectory

plots_flight_path(b195_t4)

b195 t5

Reads the data

b195_t5 <- readr::read_csv(here::here("unpaired-points", "b195-t5-unpaired-points-xyz.csv")) %>% 
  dplyr::mutate(butterfly_flight = "b195_t5") 

Calculates the variables

flights <-
  calc_flight_trajectory_vars(b195_t5) %>%
  bind_rows(flights, .)

Plots the trajectory

plots_flight_path(b195_t5)

b206 t1

Reads the data

b206_t1 <- readr::read_csv(here::here("unpaired-points", "b206-t1-unpaired-points-xyz.csv")) %>% 
  dplyr::mutate(butterfly_flight = "b206-t1") 

Calculates the variables

flights <-
  calc_flight_trajectory_vars(b206_t1) %>%
  bind_rows(flights, .)

Plots the trajectory

plots_flight_path(b206_t1)

b206 t2

Reads the data

b206_t2 <- readr::read_csv(here::here("unpaired-points", "b206-t2-unpaired-points-xyz.csv")) %>% 
  dplyr::mutate(butterfly_flight = "b206-t2") 

Calculates the variables

flights <-
  calc_flight_trajectory_vars(b206_t2) %>%
  bind_rows(flights, .)

Plots the trajectory

plots_flight_path(b206_t2)

b206 t3

Reads the data

b206_t3 <- readr::read_csv(here::here("unpaired-points", "b206-t3-unpaired-points-xyz.csv")) %>% 
  dplyr::mutate(butterfly_flight = "b206-t3") 

Calculates the variables

flights <-
  calc_flight_trajectory_vars(b206_t3) %>%
  bind_rows(flights, .)

Plots the trajectory

plots_flight_path(b206_t3)

b206 t4

Reads the data

b206_t4 <- readr::read_csv(here::here("unpaired-points", "b206-t4-unpaired-points-xyz.csv")) %>% 
  dplyr::mutate(butterfly_flight = "b206-t4") 

Calculates the variables

flights <-
  calc_flight_trajectory_vars(b206_t4) %>%
  bind_rows(flights, .)

Plots the trajectory

plots_flight_path(b206_t4)

b211 t1

Reads the data

b211_t1 <- readr::read_csv(here::here("unpaired-points", "b211-t1-unpaired-points-xyz.csv")) %>% 
  dplyr::mutate(butterfly_flight = "b211-t1") 

Calculates the variables

flights <-
  calc_flight_trajectory_vars(b211_t1) %>%
  bind_rows(flights, .)

Plots the trajectory

plots_flight_path(b211_t1)

b211 t2

Reads the data

b211_t2 <- readr::read_csv(here::here("unpaired-points", "b211-t2-unpaired-points-xyz.csv")) %>% 
  dplyr::mutate(butterfly_flight = "b211-t2") 

Calculates the variables

flights <-
  calc_flight_trajectory_vars(b211_t2) %>%
  bind_rows(flights, .)

Plots the trajectory

plots_flight_path(b211_t2)

b211 t3

Reads the data

b211_t3 <- readr::read_csv(here::here("unpaired-points", "b211-t3-unpaired-points-xyz.csv")) %>% 
  dplyr::mutate(butterfly_flight = "b211-t3") 

Calculates the variables

flights <-
  calc_flight_trajectory_vars(b211_t3) %>%
  bind_rows(flights, .)

Plots the trajectory

plots_flight_path(b211_t3)

b211 t4

Reads the data

b211_t4 <- readr::read_csv(here::here("unpaired-points", "b211-t4-unpaired-points-xyz.csv")) %>% 
  dplyr::mutate(butterfly_flight = "b211-t4") 

Calculates the variables

flights <-
  calc_flight_trajectory_vars(b211_t4) %>%
  bind_rows(flights, .)

Plots the trajectory

plots_flight_path(b211_t4)

b216 t1

Reads the data

b216_t1 <- readr::read_csv(here::here("unpaired-points", "b216-t1-unpaired-points-xyz.csv")) %>% 
  dplyr::mutate(butterfly_flight = "b216-t1") 

Calculates the variables

flights <-
  calc_flight_trajectory_vars(b216_t1) %>%
  bind_rows(flights, .)

Plots the trajectory

plots_flight_path(b216_t1)

b216 t2

Reads the data

b216_t2 <- readr::read_csv(here::here("unpaired-points", "b216-t2-unpaired-points-xyz.csv")) %>% 
  dplyr::mutate(butterfly_flight = "b216-t2") 

Calculates the variables

flights <-
  calc_flight_trajectory_vars(b216_t2) %>%
  bind_rows(flights, .)

Plots the trajectory

plots_flight_path(b216_t2)

b216 t4

Reads the data

b216_t4 <- readr::read_csv(here::here("unpaired-points", "b216-t4-unpaired-points-xyz.csv")) %>% 
  dplyr::mutate(butterfly_flight = "b216-t4") 

Calculates the variables

flights <-
  calc_flight_trajectory_vars(b216_t4) %>%
  bind_rows(flights, .)

Plots the trajectory

plots_flight_path(b216_t4)

b220 t2

Reads the data

b220_t2 <- readr::read_csv(here::here("unpaired-points", "b220-t2-unpaired-points-xyz.csv")) %>% 
  dplyr::mutate(butterfly_flight = "b220-t2") 

Calculates the variables

flights <-
  calc_flight_trajectory_vars(b220_t2) %>%
  bind_rows(flights, .)

Plots the trajectory

plots_flight_path(b220_t2)

b220 t3

Reads the data

b220_t3 <- readr::read_csv(here::here("unpaired-points", "b220-t3-unpaired-points-xyz.csv")) %>% 
  dplyr::mutate(butterfly_flight = "b220-t3") 

Calculates the variables

flights <-
  calc_flight_trajectory_vars(b220_t3) %>%
  bind_rows(flights, .)

Plots the trajectory

plots_flight_path(b220_t3)

b228 t1

Reads the data

b228_t1 <- readr::read_csv(here::here("unpaired-points", "b228-t1-unpaired-points-xyz.csv")) %>% 
  dplyr::mutate(butterfly_flight = "b228-t1") 

Calculates the variables

flights <-
  calc_flight_trajectory_vars(b228_t1) %>%
  bind_rows(flights, .)

Plots the trajectory

plots_flight_path(b228_t1)

b228 t2

Reads the data

b228_t2 <- readr::read_csv(here::here("unpaired-points", "b228-t2-unpaired-points-xyz.csv")) %>% 
  dplyr::mutate(butterfly_flight = "b228-t2") 

Calculates the variables

flights <-
  calc_flight_trajectory_vars(b228_t2) %>%
  bind_rows(flights, .)

Plots the trajectory

plots_flight_path(b228_t2)

b228 t3

Reads the data

b228_t3 <- readr::read_csv(here::here("unpaired-points", "b228-t3-unpaired-points-xyz.csv")) %>% 
  dplyr::mutate(butterfly_flight = "b228-t3") 

Calculates the variables

flights <-
  calc_flight_trajectory_vars(b228_t3) %>%
  bind_rows(flights, .)

Plots the trajectory

plots_flight_path(b228_t3)

b228 t4

Reads the data

b228_t4 <- readr::read_csv(here::here("unpaired-points", "b228-t4-unpaired-points-xyz.csv")) %>% 
  dplyr::mutate(butterfly_flight = "b228-t4") 

Calculates the variables

flights <-
  calc_flight_trajectory_vars(b228_t4) %>%
  bind_rows(flights, .)

Plots the trajectory

plots_flight_path(b228_t4)

b228 t5

Reads the data

b228_t5 <- readr::read_csv(here::here("unpaired-points", "b228-t5-unpaired-points-xyz.csv")) %>% 
  dplyr::mutate(butterfly_flight = "b228-t5") 

Calculates the variables

flights <-
  calc_flight_trajectory_vars(b228_t5) %>%
  bind_rows(flights, .)

Plots the trajectory

plots_flight_path(b228_t5)

b228 t6

Reads the data

b228_t6 <- readr::read_csv(here::here("unpaired-points", "b228-t6-unpaired-points-xyz.csv")) %>% 
  dplyr::mutate(butterfly_flight = "b228-t6") 

Calculates the variables

flights <-
  calc_flight_trajectory_vars(b228_t6) %>%
  bind_rows(flights, .)

Plots the trajectory

plots_flight_path(b228_t6)

b228 t7

Reads the data

b228_t7 <- readr::read_csv(here::here("unpaired-points", "b228-t7-unpaired-points-xyz.csv")) %>% 
  dplyr::mutate(butterfly_flight = "b228-t7") 

Calculates the variables

flights <-
  calc_flight_trajectory_vars(b228_t7) %>%
  bind_rows(flights, .)

Plots the trajectory

plots_flight_path(b228_t7)

b238 t1

Reads the data

b238_t1 <- readr::read_csv(here::here("unpaired-points", "b238-t1-unpaired-points-xyz.csv")) %>% 
  dplyr::mutate(butterfly_flight = "b238-t1") 

Calculates the variables

flights <-
  calc_flight_trajectory_vars(b238_t1) %>%
  bind_rows(flights, .)

Plots the trajectory

plots_flight_path(b238_t1)

b238 t2

Reads the data

b238_t2 <- readr::read_csv(here::here("unpaired-points", "b238-t2-unpaired-points-xyz.csv")) %>% 
  dplyr::mutate(butterfly_flight = "b238-t2") 

Calculates the variables

flights <-
  calc_flight_trajectory_vars(b238_t2) %>%
  bind_rows(flights, .)

Plots the trajectory

plots_flight_path(b238_t2)

b238 t4

Reads the data

b238_t4 <- readr::read_csv(here::here("unpaired-points", "b238-t4-unpaired-points-xyz.csv")) %>% 
  dplyr::mutate(butterfly_flight = "b238-t4") 

Calculates the variables

flights <-
  calc_flight_trajectory_vars(b238_t4) %>%
  bind_rows(flights, .)

Plots the trajectory

plots_flight_path(b238_t4)

b238 t5

Reads the data

b238_t5 <- readr::read_csv(here::here("unpaired-points", "b238-t5-unpaired-points-xyz.csv")) %>% 
  dplyr::mutate(butterfly_flight = "b238-t5") 

Calculates the variables

flights <-
  calc_flight_trajectory_vars(b238_t5) %>%
  bind_rows(flights, .)

Plots the trajectory

plots_flight_path(b238_t5)

b224 t2

Reads the data

b224_t2 <- readr::read_csv(here::here("unpaired-points", "b224-t2-unpaired-points-xyz.csv")) %>% 
  dplyr::mutate(butterfly_flight = "b224_t2") 

Calculates the variables

flights <-
  calc_flight_trajectory_vars(b224_t2) %>%
  bind_rows(flights, .)

Plots the trajectory

plots_flight_path(b224_t2)

Calculates the variables

flights <-
  calc_flight_trajectory_vars(b224_t2) %>%
  bind_rows(flights, .)

Plots the trajectory

plots_flight_path(b224_t2)

b224 t3

Reads the data

b224_t3 <- readr::read_csv(here::here("unpaired-points", "b224-t3-unpaired-points-xyz.csv")) %>% 
  dplyr::mutate(butterfly_flight = "b224_t3") 

Calculates the variables

flights <-
  calc_flight_trajectory_vars(b224_t3) %>%
  bind_rows(flights, .)

Plots the trajectory

plots_flight_path(b224_t3)

Calculates the variables

flights <-
  calc_flight_trajectory_vars(b224_t3) %>%
  bind_rows(flights, .)

Plots the trajectory

plots_flight_path(b224_t3)

b238 t3

Reads the data

b238_t3 <- readr::read_csv(here::here("unpaired-points", "b238-t3-unpaired-points-xyz.csv")) %>% 
  dplyr::mutate(butterfly_flight = "b238_t3") 

Calculates the variables

flights <-
  calc_flight_trajectory_vars(b238_t3) %>%
  bind_rows(flights, .)

Plots the trajectory

plots_flight_path(b238_t3)

Calculates the variables

flights <-
  calc_flight_trajectory_vars(b238_t3) %>%
  bind_rows(flights, .)

Plots the trajectory

plots_flight_path(b238_t3)

b238 t6

Reads the data

b238_t6 <- readr::read_csv(here::here("unpaired-points", "b238-t6-unpaired-points-xyz.csv")) %>% 
  dplyr::mutate(butterfly_flight = "b238_t6") 

Calculates the variables

flights <-
  calc_flight_trajectory_vars(b238_t6) %>%
  bind_rows(flights, .)

Plots the trajectory

plots_flight_path(b238_t6)

Calculates the variables

flights <-
  calc_flight_trajectory_vars(b238_t6) %>%
  bind_rows(flights, .)

Plots the trajectory

plots_flight_path(b238_t6)

b246 t1

Reads the data

b246_t1 <- readr::read_csv(here::here("unpaired-points", "b246-t1-unpaired-points-xyz.csv")) %>% 
  dplyr::mutate(butterfly_flight = "b246_t1") 

Calculates the variables

flights <-
  calc_flight_trajectory_vars(b246_t1) %>%
  bind_rows(flights, .)

Plots the trajectory

plots_flight_path(b246_t1)

Calculates the variables

flights <-
  calc_flight_trajectory_vars(b246_t1) %>%
  bind_rows(flights, .)

Plots the trajectory

plots_flight_path(b246_t1)

b246 t3

Reads the data

b246_t3 <- readr::read_csv(here::here("unpaired-points", "b246-t3-unpaired-points-xyz.csv")) %>% 
  dplyr::mutate(butterfly_flight = "b246_t3") 

Calculates the variables

flights <-
  calc_flight_trajectory_vars(b246_t3) %>%
  bind_rows(flights, .)

Plots the trajectory

plots_flight_path(b246_t3)

Calculates the variables

flights <-
  calc_flight_trajectory_vars(b246_t3) %>%
  bind_rows(flights, .)

Plots the trajectory

plots_flight_path(b246_t3)

Table with all the data

Making the separator in butterfly_flight homogenous and renaming it to butterfly_take

flights %>% 
  tidyr::separate(col = butterfly_flight, into = c("butterfly", "flight"), sep = "([-_])") %>% 
  tidyr::unite("butterfly_take", butterfly:flight, sep = "_") -> flights

Printing the actual table

flights %>% 
  DT::datatable(extensions = 'Buttons',
                options = list(dom = 'Blfrtip',
                               buttons = c('copy', 'csv', 'excel', 'pdf', 'print'),
                               lengthMenu = list(c(10,25,50,-1),
                                                 c(10,25,50,"All"))))